package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HJ12_去除多余空格 {

    public static void main(String[] args) throws FileNotFoundException {
        MyUtil.START();
        Solution();
        MyUtil.END();
    }

    static void Solution() throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/12.txt"));
        String s = in.nextLine().trim();
        int len = s.length();
        String[] sArray = in.nextLine().trim().split(",");
        String[] keywords = new String[sArray.length];
        for (int i = 0; i < sArray.length; i++) {
            String[] index = sArray[i].split(" ");
            keywords[i] = s.substring(Integer.parseInt(index[0]), Integer.parseInt(index[1]) + 1);
        }
        boolean flag = false;
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < len; i++) {
            char a = s.charAt(i);
            if (a == '\'') {
                flag = !flag;
            }
            if (!flag && a == ' ') {
                for (int j = i + 1; j < len; j++) {
                    char b = s.charAt(j);
                    if (b == ' ') {
                        i = j;
                    } else {
                        break;
                    }
                }
            }
            output.append(a);
        }
        String output_string = output.toString();
        List<int[]> resList = new ArrayList<>();
        for (int i = 0; i < keywords.length; i++) {
            int index1 = output_string.indexOf(keywords[i]);
            int index2 = index1 + keywords[i].length() - 1;
            resList.add(new int[] {index1, index2});
        }
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < resList.size(); i++) {
            res.append("[");
            res.append(resList.get(i)[0]);
            res.append(", ");
            res.append(resList.get(i)[1]).append("]");
        }
        System.out.println(res);
    }
}
